/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * AUTHOR(S)
 * 王海斌
 *   E-mail: wanghaibin@qiyi.com
 *
 * VERSION
 *   Mon 09 Apr 2018 10:26:17 AM CST
 */


#include <mntent.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <string>

void test1()
{
    struct mntent *m;
    FILE *f = NULL;
    f = setmntent("/etc/mtab","r"); //open file for describing the mounted filesystems
    if(!f)
        printf("error:%s\n",strerror(errno));
    while ((m = getmntent(f)))        //read next line
        printf("Drive %s, name %s,type  %s,opt  %s\n", m->mnt_dir, m->mnt_fsname,m->mnt_type,m->mnt_opts );
    endmntent(f);   //close file for describing the mounted filesystems
}

void test2(int &pos, int step)
{
    int size = 10;
    if (pos >= size) {
        pos=0;
    }
    int max=pos+step;
    while (pos < size && pos < max) {
        pos++;
        printf("cur pos=%d\n", pos);
    }
    printf("out while");
}

const char * CONST_EXT_MATA = ".qsm";
const char * CONST_FILE_SPLIT_STR ="_p_";
bool test_GetUriByLocalPath()
{
    //从/data1/www/xx/yy/cc.mm_p_0 中获取uri=/xx/yy/cc.mm
    std::string uri;
    std::string lpath = "/data1/www/xx/yy/cc.mm.qsm";
    //std::string lpath = "/data1/www/xx/yy/cc.mm";
    std::string preStr("/data1/www");
    size_t diskPos = lpath.find(preStr);
    if(diskPos==std::string::npos) {
        printf("not find\r\n");
        return false;
    }
    std::string strtmpUri(lpath.substr(preStr.size()));
    printf("xxxxxxxxxxxxx strTmp=%s\n", strtmpUri.c_str());
    size_t spMeta = strtmpUri.find(CONST_EXT_MATA);
    if(spMeta!=std::string::npos&&spMeta==strtmpUri.length()-strlen(CONST_EXT_MATA)) {
        strtmpUri.erase(spMeta,strlen(CONST_EXT_MATA));
    }
    size_t spPos = strtmpUri.find(CONST_FILE_SPLIT_STR); //分割位置
    if(spPos!=std::string::npos) {
        uri = strtmpUri.substr(0, spPos);
    } else {
        uri = strtmpUri;
    }
    printf("ori=%s uri=%s\r\n",lpath.c_str(), uri.c_str());
    std::string uri2="/data1/www/xx/yy/cc.mm_p_0";
    spPos = uri2.find(CONST_FILE_SPLIT_STR); //分割位置
    if(spPos!=std::string::npos) {
        uri2.erase(spPos);
    }
    printf("uri2=%s\r\n", uri2.c_str());
    return true;
}

//验证删除string末尾的多个/
void QEraseLastChar(std::string & data, char c)
{
    //std::string prePath("/data/bbbb////");
    std::string prePath("/////");
    if(data.size()>0) {
        const char * h = data.c_str();
        const char * last = h+(data.size()-1);
        while( *last==c && last>=h) {
            last--;
        }
        if((last-h+1) < data.size()) {
            printf("==%d\n", last-h+1);
            data.erase(last-h+1);
        }
        printf("==%s\n", data.c_str());
    }
}

void test3(int &pos_, std::string & domain) {
    std::string qname;
    while (pos_ >= 0) {
        if (domain[pos_] == '.') {
            qname = domain.substr(pos_ + 1);
            pos_--;
            break;
        }
        if(0==pos_) {
            qname = domain;
        }
        pos_--;
    }
    printf("%s\n", qname.c_str());
}
int main()
{
    std::string domain("www.baidu.comxxx.com.");
    int pos_ = domain.size()-2;
    test3(pos_, domain);
    test3(pos_, domain);
    test3(pos_, domain);
    test3(pos_, domain);
    return 0;

    std::string str("/a///");
    QEraseLastChar(str, '/');
    return 0;
    test_GetUriByLocalPath();
    return 0;
    test1();
    printf("%d\n", _SC_CLK_TCK);
    int pos = 0;
    while(1) {
        sleep(1);
        test2(pos, 2);
    }
    return 0;
}

